// pages/mine/recommend_upload/recommend_upload.js
var util = require('../../../utils/util.js');
var common = require("../../../utils/common.js");
//获取应用实例
var app = getApp()
const qiniuUploader = require("../../../utils/qiniuUploader");
//index.js

// 初始化七牛相关参数
function initQiniu() {
  var options = {
    region: 'ECN', // 华东区
    domain: 'or7ukzqb3.bkt.clouddn.com',
    uptokenURL: 'https://xiaozhi.iwares.cn/wangcai/api/qiniu/uptoken',
    // uptoken: 'xxxx',
    // key: 'customFileName.jpg',
    shouldUseQiniuFileName: true
  };
  qiniuUploader.init(options);
}

//获取应用实例
var app = getApp()
Page({
  data: {
    imageObject: {},
    title:'',
    subtitle:'',
    content:'点击下面的分享按钮(低版本微信可能无法点击分享)，或右上角转发，并成功转发到任意群即可收到链接的推送消息',
    replycontent:'',
    level:4,
    levelArray:['1级','2级','3级','4级','5级'],
    price:0,
    page:'/pages/smartlife/common_recommend/common_recommend',
    position:2,
    positionArray:['轮播图','首页列表','优惠页'],
    loading:false,
    btnText: '上传'
  },
  //事件处理函数
  onLoad: function (options) {
    var that = this;
  },
  didPressChooesImage: function () {
    var that = this;
    didPressChooesImage(that);
  },
  titleInput: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  subtitleInput: function (e) {
    this.setData({
      subtitle: e.detail.value
    })
  },
  contentInput: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  replycontentInput: function (e) {
    this.setData({
      replycontent: e.detail.value
    })
  },
  levelPickerChange: function  (e) {
    this.setData({
      level:e.detail.value
    })
  },
  priceInput: function (e) {
    this.setData({
      price: e.detail.value
    })
  },
  pageInput: function (e) {
    this.setData({
      page: e.detail.value
    })
  },
  positionPickerChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      position: e.detail.value
    })
  },
  btnUpload: function (e) {
    var that = this
    // 设置按钮加载状态
    this.setData({
      loading: true,
      btnText: '上传中…'
    })
    if (that.data.imageObject.imageURL == undefined || that.data.imageObject.imageURL == "" || that.data.imageObject.imageURL == null){
      that.setData({
        loading: false,
        btnText: '上传'
      })
      wx.showModal({
        title: '错误提示',
        content: '请先上传图片！',
        showCancel: false
      })
      return
    }
    if (that.data.title.length==0||that.data.replycontent.length==0) {
      that.setData({
        loading: false,
        btnText: '上传'
      })
      wx.showModal({
        title: '错误提示',
        content: '标题和分享内容不能为空！',
        showCancel: false
      })
      return
    }
    // console.log(that.data.title)
    // console.log(that.data.subtitle)
    // console.log(that.data.content)
    // console.log(that.data.imageObject.imageURL)
    // console.log(that.data.replycontent)
    // console.log(that.data.level)
    // console.log(that.data.price)
    // console.log(that.data.page)
    // console.log(that.data.positon)
    wx.request({
      url: common.apiUrl() + 'addRecommend',
      data:{
        title:that.data.title,
        subtitle:that.data.subtitle,
        content:that.data.content,
        img: 'http://'+that.data.imageObject.imageURL,
        replycontent:that.data.replycontent,
        level:that.data.level,
        price:that.data.price,
        link:that.data.page,
        position:that.data.position
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if(res.data.status==1){
          wx.showToast({
            title: '上传成功',
            icon: 'success'
          })
          //刷新上一页数据
          var pages = getCurrentPages();
          if (pages.length > 1) {
            //上一个页面实例对象
            var prePage = pages[pages.length - 2];
            prePage.onPullDownRefresh()
          }
          wx.navigateBack({
            delta: 1
          })
        }else{
          wx.showModal({
            title: '错误提示',
            content: res.data.msg,
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                //刷新上一页数据
                var pages = getCurrentPages();
                if (pages.length > 1) {
                  //上一个页面实例对象
                  var prePage = pages[pages.length - 2];
                  prePage.onPullDownRefresh()
                }
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
        that.setData({
          loading: false,
          btnText: '上传'
        })
      }
    })
  }
});

function didPressChooesImage(that) {
  initQiniu();
  // 微信 API 选文件
  wx.chooseImage({
    count: 1,
    success: function (res) {
      var filePath = res.tempFilePaths[0];
      // 交给七牛上传
      qiniuUploader.upload(filePath, (res) => {
        that.setData({
          'imageObject': res
        });
      }, (error) => {
        console.error('error: ' + JSON.stringify(error));
      }
        // , {
        //   region: 'ECN', // 华东区
        //   uptokenURL: 'https://xiaozhi.iwares.cn/wangcai/api/qiniu/uptoken',
        //   // uptoken: 'xxxx',
        //   domain: 'or7ukzqb3.bkt.clouddn.com',
        //   shouldUseQiniuFileName: false
        // }
      );
    }
  })
}